package com.crr.utils;

import java.sql.*;

public class DBUtils {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/beiyou";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "chen1116.";

    /**
     * 获取连接
     * @return
     */
    public static Connection getConn(){
        try {
            //2.加载驱动
            Class.forName(DRIVER);
            //3.获取连接
            Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            return conn;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void closeTwo(PreparedStatement ps, Connection conn){

        try {
            if(ps!=null)
                ps.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            if(conn!=null)
                conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void closeAll(ResultSet rs, PreparedStatement ps, Connection conn){

        try {
            if(ps!=null)
                ps.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            if(conn!=null)
                conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
